package com.jjwxc.jwjskandriod.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.AdolescentActivity;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;

public class PopViewUtil {


    public static void initPopWindow(View v, Context context, PopDismissListener popDismissListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.bookshelf_pop_item, null, false);

        LinearLayout btn_web = (LinearLayout) view.findViewById(R.id.ll_fengmian);
        LinearLayout btn_wap = (LinearLayout) view.findViewById(R.id.ll_list);
        ImageView img_one = (ImageView) view.findViewById(R.id.img_one);
        ImageView img_two = (ImageView) view.findViewById(R.id.img_two);
        TextView tv_one = (TextView) view.findViewById(R.id.tv_one);
        TextView tv_two = (TextView) view.findViewById(R.id.tv_two);
        int type = PreUtils.getInt("ViewType", 1);
        if (1 == type) {
            img_one.setImageResource(R.mipmap.fengmianmoshi_icon_true);
            tv_one.setTextColor(Color.parseColor("#4D998D"));
            img_two.setImageResource(R.mipmap.listmodel_false);
            tv_two.setTextColor(Color.parseColor("#4E4E4E"));
        } else {
            img_one.setImageResource(R.mipmap.fengmianmoshi_icon_false);
            tv_one.setTextColor(Color.parseColor("#4E4E4E"));
            img_two.setImageResource(R.mipmap.listmodel_true);
            tv_two.setTextColor(Color.parseColor("#4D998D"));
        }
        //1.构造一个PopupWindow，参数依次是加载的 View，宽高

        final PopupWindow popWindow = new PopupWindow(view,
                460, 360, true);

        popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画

        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效

        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAsDropDown(v, 20, 0);
        //设置 popupWindow 里的按钮的事件
        btn_web.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type != 1) {
                    popDismissListener.onOkClick("1");
                    popWindow.dismiss();
                }

            }
        });
        btn_wap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type != 2) {
                    popDismissListener.onOkClick("2");
                    popWindow.dismiss();
                }
            }
        });
    }


    public interface PopDismissListener {
        void onOkClick(String group);
    }



    public static void adolescentPop(View v, Activity context) {
        View view = LayoutInflater.from(context).inflate(R.layout.adolescent_pop, null, false);
        TextView tv_adolescent = view.findViewById(R.id.tv_adolescent);
        TextView tv_adole_ok = view.findViewById(R.id.tv_adole_ok);
        tv_adolescent.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.PopupWindow);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(() -> setBackgroundAlpha(1, context));
        tv_adole_ok.setOnClickListener(v1 -> {
            popWindow.dismiss();
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

        setBackgroundAlpha(0.3f, context);
        tv_adolescent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AdolescentActivity.class);
                context.startActivity(intent);
                popWindow.dismiss();
            }
        });
    }

    public static void sendGifePop(View v, Context context, String value) {
        View view = LayoutInflater.from(context).inflate(R.layout.send_gife_pop, null, false);
        TextView tv_gife = view.findViewById(R.id.tv_gife);

        ConstraintLayout cl_gife = view.findViewById(R.id.cl_gife);

        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

        popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画

        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setClippingEnabled(false);
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效

        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
        Spanned spanned = Html.fromHtml("您目前排在<font color= #4D998D>" + value + "</font>名");
        tv_gife.setText(spanned);
        cl_gife.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });

    }


    public static void stackAllPop(View v, Activity context, String text, PopDismissListener popDismissListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.stack_all_pop, null, false);
        TextView tv_one = view.findViewById(R.id.tv_one);
        tv_one.setText(text);
        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                FFUtils.getPx(80), FFUtils.getPx(30), true);

        popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画

        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效

        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAsDropDown(v, 0, -15);
        //设置 popupWindow 里的按钮的事件
        tv_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popDismissListener.onOkClick("1");
                popWindow.dismiss();
            }
        });

    }

    //设置屏幕背景透明效果
    public static void setBackgroundAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }
}
